package com.yyge.controller;

import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 测试控制器
 */
@RestController
@RequestMapping("/test")
@CrossOrigin
//指定缓存配置的名称
@CacheConfig(cacheNames = "commonCache")
public class TestController {

    @PostMapping("/getData")
    @Cacheable(value = "dataCache", key = "#params.get('dataId')",condition = "#params.get('dataId')!= null")
    public String getData(@RequestBody Map<String, String> params) {
        String dataId = params.get("dataId");
        //do something
        //模拟加载时间 2秒钟
        try {
            Thread.sleep(2000);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "data:" + dataId;
    }
}
